function [X, Xnames] = transformMatrix(X, X_transfs, Xnames, varargin)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% This function transforms variables and the names associated.
	%
	% Note: the transformation codes are not consistent with calc_GLM_elasticities.m.
	%	0: rescaleMatrix (divide / multiply by a power of 10)
	%	1:  x --> log(x)
	%	4:  x --> log(1+x)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Inputs:
	% X:				NumRows x NumX
	% X_transfs:		1 x NumX
	% Xnames:			cell(NumX,1) of strings
	% varargin{1} = denominator (if transf code == 2 or transf code == 5)
	% varargin{2} = denominatorName (if transf code == 2 or transf code == 5)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Outputs:
	% 
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	NumX = size(X, 2);
	
	for oo = 1:NumX
		mytransf = X_transfs(oo);
		if mytransf == 0	% rescale: x/1e?
			[X(:,oo), ~, Xnames(oo)] = rescaleMatrix(X(:,oo), Xnames(oo));
		end
		if mytransf == 1	% log(x)
			[X(:,oo), Xnames(oo)] = takeLogTransform(X(:,oo), 0, Xnames(oo));
		end
		if mytransf == 2	% x / denominator
			denominator = varargin{1}; % NumRows x 1
			denominatorName = varargin{2};
			X(:,oo) = X(:,oo)./denominator;
			Xnames(oo) = sprintf('%s/%s', Xnames{oo}, denominatorName);
		end
		if mytransf == 4	% log(1+x)
			[X(:,oo), Xnames(oo)] = takeLogTransform(X(:,oo), 1, Xnames(oo));
		end
		if mytransf == 5  % log((1+x)/denominator)
			denominator = varargin{1}; % NumRows x 1
			denominatorName = varargin{2};
			X(:,oo) = log((X(:,oo)+1)./denominator);
			Xnames{oo} = sprintf('log((1+%s)/%s)', Xnames{oo}, denominatorName);
		end
	end	
end
